package com.jsnkj.basic.fragment.earnings

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.DailyBillingAdapter
import com.jsnkj.basic.databinding.FragmentDailyBillingBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.jsnkj.basic.util.ErrorResultUtil

//日账单
class DailyBillingFragment :
    BaseMVVMFragment<FragmentDailyBillingBinding, DailyBillingViewModel>() {

    companion object {
        fun newInstance() = DailyBillingFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_daily_billing

    var dailyAdapter: DailyBillingAdapter? = null


    override fun initView(savedInstanceState: Bundle?) {

        mVM.postHttpResult.observe(this) { entity ->

            mVDB.txtAggregateIncome.text = "${entity.getTotal_commissions()}"

            if (entity.getBills().isNullOrEmpty()) {
                entity.setBills(ArrayList())
            }

            dailyAdapter = DailyBillingAdapter(requireActivity(), entity.getBills()!!)

            mVDB.txtError.visibility = View.GONE

            mVDB.rvDaily.apply {
                layoutManager =
                    LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
                adapter = dailyAdapter
                visibility = View.VISIBLE
                setFocusable(false);
            }
        }


        mVM.errorResult.observe(this) {
            ErrorResultUtil.errorResult(requireActivity() as BaseMVVMActivity<*, *>, it)

            mVDB.txtError.visibility = View.VISIBLE
            mVDB.rvDaily.visibility = View.GONE
        }

        mVM.postAppList()
    }

}